﻿mainModule.factory('localizationCacheSvc', ["$cacheFactory", "$window", "settings", function ($cacheFactory, $window, settings) {
    "use strict";
    var cache = $cacheFactory('translateCache');
   
    function getCulture() {
        if (!$window.sessionStorage.currentCulture)
            $window.sessionStorage.currentCulture = settings.defaultCultureCode;

        return $window.sessionStorage.currentCulture;
    }

    function setCulture(culture){
        $window.sessionStorage.currentCulture = culture;
    }

    function get(key) {
        var localeString = cache.get("T" + getCulture() + "-" + key);
        if (typeof (localeString) === "undefined")
            return key;
        else
            return localeString;
    };
    function put(culture, key, value) { cache.put("T" + culture + "-" + key, value); };

    return {
        init: function (list) {
            var i;
            if (typeof (list) !== "undefined")
                for (i = 0; i < list.length; i++) {
                    put(list[i].CultureCode, list[i].Name, list[i].Value);
                }            
        },
        get: get,
        put : put,
        getCulture: getCulture,
        setCulture: setCulture
    }
}]);